#include <TFT_eSPI.h>

#define TFT_bl 15 // LED back-light control pin

// 定义 TFT_eSPI 库的对象
TFT_eSPI tft = TFT_eSPI();
// 屏幕尺寸
const int SCREEN_WIDTH = 170;
const int SCREEN_HEIGHT = 320;

// 圆的参数
const int LARGE_CIRCLE_RADIUS = 60;
const int SMALL_CIRCLE_RADIUS = 40;
const int LARGE_CIRCLE_Y = 90;
const int SMALL_CIRCLE_Y = 198;

class TextDisplayer
{
private:
  int lastTextX;
  int lastTextY;
  int lastTextWidth;
  int lastTextHeight;
  String lastTextdata;
  uint32_t lastTextcolor;

public:
  TextDisplayer() : lastTextX(0), lastTextY(0), lastTextWidth(0), lastTextHeight(0), lastTextdata(""), lastTextcolor(0) {}

  void display(int x, int y, String data, int size, int font, uint32_t color)
  {
    if (lastTextdata != data || lastTextcolor != color)
    {
      // 获取当前文字尺寸
      tft.setTextSize(size);
      tft.setTextFont(font);
      tft.setTextColor(color, TFT_BLACK); // 字体颜色/背景色
      int textWidth = tft.textWidth(data);
      int textHeight = tft.fontHeight();
      // 计算当前文字位置
      int textX = x - textWidth / 2;
      int textY = y - textHeight / 2;

      // 用矩形清空上一次文字显示区域
      if (lastTextX != 0 && lastTextY != 0)
      {
        tft.fillRect(lastTextX, lastTextY, lastTextWidth, lastTextHeight, TFT_BLACK);
      }
      // 显示新文字
      tft.drawString(data, textX, textY);

      // 更新上一次绘制字符区域的相关信息
      lastTextcolor = color;
      lastTextdata = data,
      lastTextX = textX;
      lastTextY = textY;
      lastTextWidth = textWidth;
      lastTextHeight = textHeight;
      tft.setTextColor(TFT_WHITE, TFT_BLACK); // 字体颜色/背景色
    }
  }
};
TextDisplayer displayer0, displayer1, displayer2, displayer3, displayer4, displayer5, displayer6, displayer7;

void tftPrintf(String data)
{
  displayer0.display(SCREEN_WIDTH / 2, 12, data, 2, 1, TFT_WHITE);
}

// 定义函数用于打印时间
void print_current_time()
{
  struct tm time_info;
  time_t now;
  time(&now);
  localtime_r(&now, &time_info);
  int hours = time_info.tm_hour;
  int minutes = time_info.tm_min;
  int seconds = time_info.tm_sec;
  char time_str[9];
  sprintf(time_str, "%02d:%02d:%02d", hours, minutes, seconds);
  tftPrintf(time_str);
}

void inittft()
{
  analogWrite(TFT_bl, 0);
  tft.begin();
  tft.setRotation(0);                     // 设置屏幕旋转角度，0 表示正常方向
  tft.fillScreen(TFT_BLACK);              // 用黑色填充屏幕
  tft.setTextColor(TFT_WHITE, TFT_BLACK); // 字体颜色/背景色
  tft.drawCentreString("Waiting", 85, LARGE_CIRCLE_Y - 20, 4);
  tft.drawCentreString("to", 85, LARGE_CIRCLE_Y + 10, 4);
  tft.drawCentreString("connect", 85, LARGE_CIRCLE_Y + 40, 4);
  delay(100);
  analogWrite(TFT_bl, nightBrightness);
}

void setuptft()
{
  tft.fillScreen(TFT_BLACK); // 用黑色填充屏幕
  tft.drawCentreString("Min", 30, 255, 2);
  tft.drawCentreString("Diff", 85, 240, 2);
  tft.drawCentreString("Max", 140, 255, 2);
  tft.drawRoundRect(0, 25, 170, 293, 30, TFT_WHITE); // 空心圆角矩形  // 设定圆角半径，这里设为30，可按需调整
  tft.drawFastHLine(0, 158, 170, TFT_WHITE);
  tft.drawFastHLine(0, 236, 170, TFT_WHITE);
  tft.drawFastHLine(60, 283, 50, TFT_WHITE);
  tft.drawFastVLine(85, 158, 78, TFT_WHITE);
}

void qidongtft()
{
  // batteryLevel = random(0, 200);
  // voltage = random(0, 10000);
  // current = random(0, 1000);
  // minVoltage = random(0, 2000);
  // maxVoltage = random(0, 2000);
  // voltageDiff = random(0, 200);
  static int Used = batteryLevel;

  // 显示更新后的变量值
  displayer1.display(SCREEN_WIDTH / 2, LARGE_CIRCLE_Y, String(batteryLevel), 2, 6, batteryLevel <= 10 ? TFT_RED : TFT_GREEN);
  tft.setTextSize(1);
  tft.drawCentreString(String(Used), SCREEN_WIDTH / 2 - 60, 125, 4);
  tft.drawCentreString("Soc", SCREEN_WIDTH / 2, 125, 4);
  tft.drawCentreString(String(abs(Used - batteryLevel)), SCREEN_WIDTH / 2 + 60, 125, 4);

  displayer2.display(SCREEN_WIDTH / 2 - 41, SMALL_CIRCLE_Y, String(voltage / 100), 1, 6, voltage <= 6500 ? TFT_RED : TFT_WHITE);
  tft.setTextSize(1);
  tft.drawCentreString("Vol", SCREEN_WIDTH / 2 - 40, 215, 2);

  displayer3.display(SCREEN_WIDTH / 2 + 41, SMALL_CIRCLE_Y, String(current / 100), 1, 6, current <= -6000 ? TFT_RED : TFT_WHITE);
  tft.setTextSize(1);
  tft.drawCentreString("Cur", SCREEN_WIDTH / 2 + 42, 215, 2);

  displayer4.display(30, 285, String(minVoltage / 1000.00), 1, 4, TFT_WHITE);
  displayer5.display(140, 285, String(maxVoltage / 1000.00), 1, 4, TFT_WHITE);
  displayer6.display(85, 270, String(voltageDiff), 1, 4, voltageDiff >= 100 ? TFT_RED : TFT_WHITE);
  displayer7.display(85, 302, String(temperature), 1, 4, TFT_WHITE);
}